package barcode.android.dao.impl;

import junit.framework.Assert;
import barcode.android.activity.Barcode_androidActivity;
import barcode.android.dao.OrganizationDao;
import barcode.android.dao.impl.BaseDaoImpl;
import barcode.android.dao.impl.SqliteDatabaseState;
import barcode.android.model.Organization;
import android.test.ActivityInstrumentationTestCase2;

public class OrganizationDaoTest extends
		ActivityInstrumentationTestCase2<Barcode_androidActivity> {

	public OrganizationDaoTest() {
		super(Barcode_androidActivity.class);
	}

	public void setUp() {
		BaseDaoImpl.initContext(getActivity().getBaseContext());
	}

	public void testAddOrganization() {
		Organization org = new Organization();
		org.setName("组织部");
		org.setEmail("email@email.com");
		org.setAbstracts("一个备注");
		org.setOwn(false);
		org.setPhone("13222222222");
		org.setQq("323424245");

		OrganizationDao dao = new OrganizationDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.add(org);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testQueryOrganization() {
		OrganizationDao dao = new OrganizationDaoImpl();
		Organization org = null;
		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			org = dao.query(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertNotNull(org);
		Assert.assertEquals("组织部", org.getName());
		Assert.assertEquals("email@email.com", org.getEmail());
		Assert.assertEquals("一个备注", org.getAbstracts());
		Assert.assertEquals(false, org.isOwn());
		Assert.assertEquals("13222222222", org.getPhone());
		Assert.assertEquals("323424245", org.getQq());
	}

	public void testDeleteOrganization() {
		OrganizationDao dao = new OrganizationDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.delete(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testUpdateOrganization() {
		Organization org = new Organization();
		org.setId(1);
		org.setName("宣传部");
		org.setEmail("ssssss@ddd.com");
		org.setAbstracts("备注一枚");
		org.setOwn(true);
		org.setPhone("13823782738");
		org.setQq("54389892");

		OrganizationDao dao = new OrganizationDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.update(org);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);

		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			org = dao.query(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertNotNull(org);
		Assert.assertEquals("宣传部", org.getName());
		Assert.assertEquals("ssssss@ddd.com", org.getEmail());
		Assert.assertEquals("备注一枚", org.getAbstracts());
		Assert.assertEquals(true, org.isOwn());
		Assert.assertEquals("13823782738", org.getPhone());
		Assert.assertEquals("54389892", org.getQq());
	}
}
